/**
 * Copyright 2009 cestum.org
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.cestum.cave.ui.components
{
import flash.display.Bitmap;
import flash.display.BitmapData;
import flash.display.BitmapDataChannel;
import flash.display.BlendMode;
import flash.display.Sprite;
import flash.events.Event;
import flash.geom.Matrix;
import flash.geom.Point;
import flash.geom.Rectangle;
import flash.text.TextField;
import flash.text.TextFormat;

import mx.core.UIComponent;
[Event(name="update", type="flash.events.Event")]

[Style(name="fillColor", type="Number", format="Color", inheriting="no")]
[Style(name="useGradient", type="Boolean", inheriting="no")]
[Style(name="embedFont", type="Boolean", inheriting="no")]
[Style(name="gradientColors", type="Array", format="Color", inheriting="no")]
[Style(name="wavePeaks", type="uint", inheriting="no")]
[Style(name="maxWavePeak", type="Number", inheriting="no")]
[Style(name="minWavePeak", type="Number", inheriting="no")]
[Style(name="waveCenter", type="Number", inheriting="no")]
public class LogoGradientText extends UIComponent
{
	public function LogoGradientText()
	{
		super();
	}
	
	public static const UPDATE:String="update";
	
	private var text_field:TextField;
	private var tf_bitmap:Bitmap;
	
	private var _embed_font:Boolean=false;
	private var _logo_text_format:TextFormat;
	private var _text_format_changed:Boolean=false;
	public function setTextFormat(tf:TextFormat,embed_font:Boolean=false):void{
		_logo_text_format=tf;
		_text_format_changed=true;
		_embed_font=embed_font;
		invalidateProperties();
		invalidateSize();
		invalidateDisplayList();
	}
	public function getTextFormat():TextFormat{
		if(_logo_text_format==null)setDefaultTextFormat();
		return _logo_text_format;
	}
	
	//TEXT COLOR/GRADIENT DEFAULT STYLE VALUES
	public function get fill_color():Number{
		return getStyle("fillColor")==null ? 0x055699 : getStyle("fillColor");
	}
	public function get use_gradient():Boolean{
		return getStyle("useGradient")==null ? false : getStyle("useGradient");
	}
	public function get gradient_colors():Array{
		return getStyle("gradientColors")==null ? new Array(0x055699,0x7FBCE8) : getStyle("gradientColors");
	}
	
	//WAVE DEFAULT STYLE VALUES
	public function get wave_peaks():Number{
		return getStyle("wavePeaks")==null ? 1 : getStyle("wavePeaks");
	}
	public function get max_wave_peak():Number{
		return getStyle("maxWavePeak")==null ? 1 : getStyle("maxWavePeak");
	}
	public function get min_wave_peak():Number{
		return getStyle("minWavePeak")==null ? 0 : getStyle("minWavePeak");
	}
	public function get wave_center():Number{
		return getStyle("waveCenter")==null ? 0.5 : getStyle("waveCenter");
	}        
	
	private var _text:String="undefined";
	private var _text_changed:Boolean=false;
	[Bindable]
	public function set text(t:String):void{
		_text=t;
		_text_changed=true;
		invalidateProperties();
		invalidateSize();
		invalidateDisplayList();
	}
	public function get text():String{return _text;}
	
	
	override protected function createChildren():void{
		super.createChildren();
		render();
	}
	
	override public function styleChanged(prop:String):void{
		//mx.controls.Alert.show("Web2PointOGradientText()::StyleChanged() prop:"+prop);
		super.styleChanged(prop);
		var update_render:Boolean=false;
		var update:Boolean=false;
		switch(prop){
			//textFormat props
			case "fillColor":
				_logo_text_format.color=getStyle("fillColor");
				//wave props
			case "wavePeaks":
			case "maxWavePeak":
			case "minWavePeak":
			case "waveCenter":
				//gradient props
			case "gradientColors":
			case "useGradient":
				update_render=true;
				update=true;
				break;
		}
		if(update_render)render();
		if(update){
			invalidateProperties();
			invalidateSize();
			invalidateDisplayList();
		}
	}
	
	override protected function commitProperties():void{
		super.commitProperties();
		var update:Boolean=false;
		if(_text_changed){
			_text_changed=false;
			update=true;
		}
		if(_text_format_changed){
			_text_format_changed=false;
			update=true;
		}
		if(update)render();
	}
	
	override protected function measure():void{
		super.measure();
		this.measuredWidth=this.measuredMinWidth=tf_bitmap.width;
		this.measuredHeight=this.measuredMinHeight=tf_bitmap.height;
	}
	
	override protected function updateDisplayList(unscaled_width:Number,unscaled_height:Number):void{
		super.updateDisplayList(unscaled_width,unscaled_height);
		dispatchEvent(new Event(UPDATE));
	}
	
	private function render():void{
		if(tf_bitmap!=null)removeChild(tf_bitmap);
		text_field=createTextField();
		var tf_holder:Sprite=new Sprite();
		tf_holder.addChild(text_field);
		
		var m:Matrix=new Matrix(1,0,0,1,0,0);
		var w:Number=tf_holder.getBounds(text_field).width;
		var h:Number=tf_holder.getBounds(text_field).height;
		var bm:BitmapData=new BitmapData(w,h,true,0);
		bm.draw(text_field,m,null,null,null,true);
		
		if(use_gradient){
			var gradient:Gradient=new Gradient(this.gradient_colors);
			gradient.render(w,h);
			var gradient_bm:BitmapData=new BitmapData(w,h);
			gradient_bm.draw(gradient,m,null,null,null,true);
			gradient_bm.copyChannel(bm,new Rectangle(0,0,w,h),new Point(),BitmapDataChannel.ALPHA,BitmapDataChannel.ALPHA);
			bm.draw(gradient_bm,m,null,null,null,true);
		}
		
//		var wave:WaveGraphic=new WaveGraphic();
//		wave.wave_peaks=this.wave_peaks;
//		wave.max_wave_peak=this.max_wave_peak;
//		wave.min_wave_peak=this.min_wave_peak;
//		wave.wave_center=this.wave_center;
//		//mx.controls.Alert.show("peaks:"+this.wave_peaks+",max_peak:"+this.max_wave_peak+",min_peak:"+this.min_wave_peak+",center:"+this.wave_center);
//		wave.bottom_color=0x000000;
//		
//		wave.top_color=0x666666;
//		wave.render(w,h);
		
		//Create gradient text, copy alpha channel to gradient box using BitmapDataChannel
		var wave_bmd:BitmapData=new BitmapData(w,h,true,0);
//		wave_bmd.draw(wave,m,null,null,null,true);
		wave_bmd.copyChannel(bm,new Rectangle(0,0,w,h),new Point(),BitmapDataChannel.ALPHA,BitmapDataChannel.ALPHA);
		
		bm.draw(wave_bmd,m,null,BlendMode.SCREEN,null,true);
		
		tf_bitmap=new Bitmap(bm);
		
		addChild(tf_bitmap);
		
	}
	
	private function createTextField():TextField{
		var tf:TextField=new TextField();
		tf.autoSize="left";
		tf.text=_text;
		tf.selectable=false;
		tf.embedFonts=_embed_font;
		tf.setTextFormat(getTextFormat());
		return tf;
	}
	
	private function setDefaultTextFormat():void{
		_logo_text_format=new TextFormat();
		//logo_text_format.font="Arial Black";
		_logo_text_format.font="Arial";
		_logo_text_format.size="35";
		_logo_text_format.bold=true;
		_logo_text_format.color=fill_color;
	}

}
}